package no.nordicsemi.android.nrfmesh.viewmodels;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import java.util.List;
import java.util.concurrent.Executors;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import no.nordicsemi.android.nrfmesh.database.AppDatabase;
import no.nordicsemi.android.nrfmesh.database.dao.SentProfileDao;
import no.nordicsemi.android.nrfmesh.database.entity.SentProfile;

public class SentProfileViewModel extends AndroidViewModel {

    private final SentProfileDao sentProfileDao;

    public SentProfileViewModel(@NonNull Application application) {
        super(application);
        AppDatabase db = AppDatabase.getInstance(application);
        sentProfileDao = db.sentProfileDao();
    }

    public LiveData<List<SentProfile>> getAllSentProfilesLive() {
        return sentProfileDao.getAllProfilesLive();
    }

    /** 异步插入 */
    public void insertSentProfile(SentProfile profile) {
        new Thread(() -> sentProfileDao.insertSentProfile(profile)).start();
    }

    /** 同步返回所有——请放到子线程里调用 */
    public List<SentProfile> getAllSentProfiles() {
        return sentProfileDao.getAllSentProfiles();
    }

    /** 同步根据 id 返回一条——请放到子线程里调用 */
    public SentProfile getSentProfileById(int id) {
        return sentProfileDao.getSentProfileById(id);
    }

    /** 判断是否重复（已去除 totalSegment 和 currentBlock） */
    public LiveData<Boolean> isDuplicate(SentProfile profile) {
        MutableLiveData<Boolean> result = new MutableLiveData<>();
        Executors.newSingleThreadExecutor().execute(() -> {
            SentProfile existing = sentProfileDao.findDuplicateProfile(
                    profile.segmentDuration,
                    profile.waveformType,
                    profile.pulseWidth,
                    profile.frequency
            );
            result.postValue(existing != null);
        });
        return result;
    }
    public void deleteSentProfile(SentProfile profile) {
        new Thread(() -> sentProfileDao.deleteSentProfile(profile)).start();
    }

    // 同步检查重复
    public boolean isDuplicateSync(SentProfile profile) {
        SentProfile existing = sentProfileDao.findDuplicateProfile(
                profile.segmentDuration,
                profile.waveformType,
                profile.pulseWidth,
                profile.frequency
        );
        return existing != null;
    }

    // 同步插入
    public void insertSentProfileSync(SentProfile profile) {
        sentProfileDao.insertSentProfile(profile);
    }

    // 根据组ID查询所有段参数
    public List<SentProfile> getProfilesByGroupId(String groupId) {
        return sentProfileDao.getProfilesByGroupId(groupId);
    }

    // 删除整个组的所有段参数
    public void deleteProfilesByGroupId(String groupId) {
        new Thread(() -> sentProfileDao.deleteProfilesByGroupId(groupId)).start();
    }

    // 检查配置名称是否重复
    public boolean isConfigNameExists(String configName) {
        return sentProfileDao.checkConfigNameExists(configName) > 0;
    }

    // 根据配置名称查询组配置
    public List<SentProfile> getProfilesByConfigName(String configName) {
        return sentProfileDao.getProfilesByConfigName(configName);
    }

    // 获取最新插入的配置（用于获取上一次使用的配置名称）
    public SentProfile getLatestProfile() {
        return sentProfileDao.getLatestProfile();
    }
}

